<script setup lang="ts">
import { ref } from "vue";
import type Typings from "@/typings";
import { $singleMsg } from "xt-public/plugins/utils/element.ts";
import portService from "@/apis";

interface PropType {
  type: "" | "job" | "develop" | "life";
}

const props = withDefaults(defineProps<PropType>(), {
  type: "",
});

const emit = defineEmits(["confirm"]);

const visible = ref(false);
const checked = ref<string>("");
const selectRows = ref<Typings.Develop.develop[]>([]);

const options = ref([
  { name: "删除", code: "delete" },
  { name: "置顶", code: "top" },
  { name: "取消置顶", code: "cancelTop" },
  { name: "转完成", code: "finish" },
  { name: "转待办", code: "cancelFinish" },
]);

const open = (rows: Typings.Develop.develop[]) => {
  visible.value = true;
  selectRows.value = rows;
};

const close = () => {
  visible.value = false;
};

const reset = () => {
  checked.value = "";
};

const handleConfirm = async () => {
  if (!props.type) {
    return $singleMsg({ type: "warning", message: "请选择待办类型" });
  }
  if (!checked.value) {
    return $singleMsg({ type: "warning", message: "请选择操作" });
  }
  if (!selectRows.value?.length) {
    return $singleMsg({ type: "warning", message: "请选择要操作的数据" });
  }

  const apiPathObj = {
    develop: portService.developBatchApi,
    job: portService.jobBatchApi,
    life: portService.lifeBatchApi,
  };

  const ids = selectRows.value.map((item) => item.id).join(",");
  await apiPathObj[props.type]({ ids, type: checked.value });
  close();
  reset();
  emit("confirm");
  $singleMsg({ type: "success", message: "操作成功" });
};

defineExpose({
  open,
});
</script>

<template>
  <xt-dialog v-model="visible" title="批量处理" :before-close="close">
    <div class="p-[20px]">
      <xt-radio v-model="checked" :options="options"></xt-radio>
    </div>

    <template #footer>
      <div class="w-full center">
        <xt-button @click="handleConfirm">确 定</xt-button>
        <xt-button type="default" @click="close">取 消</xt-button>
      </div>
    </template>
  </xt-dialog>
</template>
